package solution._0010._0019;

import common.ListNode;

/**
 * Created by chen_snow
 * Date: 2021/5/31 23:11
 * Description: 删除链表的倒数第 N 个结点
 */
public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode zero = new ListNode(0);
        zero.next = head;

        ListNode faster = zero;
        ListNode slower = zero;

        for (int i = 0; i < n; i++) {
            faster = faster.next;
        }

        while (faster.next != null) {
            faster = faster.next;
            slower = slower.next;
        }

        slower.next = slower.next.next;
        return zero.next;
    }
}
